package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zerox.domain.TableVO;
import com.zerox.entity.Supplier;
import com.zerox.entity.SysUser;
import com.zerox.service.SysUserService;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/supplier")
public class SupplierController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping(value = "/tosupplierList")
    public String toString() {
        return "supplier/supplierList";
    }

    @GetMapping(value = "supplierList", produces = "application/json;charset=utf-8") //servlet
    @ResponseBody
    public String listSupplier(Integer page, Integer limit, String supName) {
        int count = sysUserService.count2(supName);
        List<Supplier> listSupplier = sysUserService.listSupplier(page, limit, supName);
        TableVO tableVO = new TableVO(0, count, listSupplier, "");

        String json = JSON.toJSONString(tableVO);

        return json;
    }


    @GetMapping(value = "/supplierView2/{id}")
    public String view(@PathVariable("id") Integer id, Model model) {
        Supplier supplier = sysUserService.selectById(id);
        model.addAttribute(supplier);

        return "supplier/supplierView";
    }


    @GetMapping(value = "toAdd")
    public String add() {
        return "supplier/add";
    }

    @PostMapping("add")
    public String supplierAdd(Supplier supplier, Model model, HttpServletRequest request, MultipartFile idPic) throws IOException {
        if (idPic.getSize()>2000000){
            model.addAttribute("idPicError","文件超过2M不能上传");
            return "add";
        }
        // 指定文件存储的目录
        String suffix = idPic.getOriginalFilename().substring(idPic.getOriginalFilename().lastIndexOf("."));

        if (".jpg.png".indexOf(suffix)==-1){
            model.addAttribute("idPicError","文件格式不正确，不能上传");
            return "add";
        }

        String path = request.getSession().getServletContext().getRealPath("statics")+ File.separator+"upload";  // ?
        String fileName = RandomUtils.nextInt(1000000)+"_pic"+suffix;
        File fileTo = new File(path+File.separator+fileName);

        //如果文件不存在就创建
        if (!fileTo.exists()){
            fileTo.mkdirs();
        }
        idPic.transferTo(fileTo);

        supplier.setIdpicpath(fileName);

        int row = sysUserService.supplierAdd(supplier);

        if (row > 0) {
            return "redirect:tosupplierList";
        }
        return "add";

    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    public String supplierDelete(@PathVariable("id") Integer id, Model model) {
        int row = sysUserService.supplierDelete(id);
        return row > 0 ? "1" : "0";
    }


    @GetMapping(value = "/toupdate/{id}")
    public String selectById(@PathVariable("id") Integer id, Model model) {
        Supplier supplier = sysUserService.selectById(id);
        model.addAttribute(supplier);

        return "supplier/update";
    }


    @PostMapping(value = "/update")
    public String update(Model model, Supplier supplier) {
        int row = sysUserService.supplierUpdate(supplier);

        if (row > 0) {
            return "redirect:tosupplierList";
        }
        model.addAttribute("errorMsg", "修改失败");

        return "update";

    }


    @GetMapping("view/{supCode}")
    @ResponseBody
    public String supCode(@PathVariable("supCode") String supCode) {
        Supplier supplier = sysUserService.selectBySupplierName(supCode);
        if (supplier == null) {
            return "0";
        }
        return "1";
    }




}
